﻿Imports dooll.Config
Imports dooll.Common
Imports Newtonsoft.Json

Namespace UI

    Public Class AdminPage
        Inherits MasterPage

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            EnableWebCache = False

            Dim XMLHTTP_MODE As String = Request.Headers.Get("HTTP_X_XMLHTTP_MODE")
            If Not String.IsNullOrEmpty(XMLHTTP_MODE) AndAlso XMLHTTP_MODE.ToLower = "true" Then
                IsXmlHttp = True
            End If
            Select Case uAction.ToLower
                'Case "xmlhttp"
                '    IsXmlHttp = True
                '    ReWriteHtmlFile()
                Case "cityflypost"
                    IsXmlHttp = True
                    ReWriteHtmlFile()
            End Select
        End Sub

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.CacheControl = "no-cache"
            IsCheckLogin = False
            Select Case uAction.ToLower
                Case "httpchklogin"
                    Call IsBasePath() '检查路径
                    If Not ChkVaidCode() Then
                        Response.Clear()
                        Response.Write(Util.ReturnValue("103", "请重新填写验证码(验证码错误)!!"))
                        Response.End()
                    End If
                    Dim txtUid As String = Request.Form("mLoginUid")
                    Dim txtPwd As String = Request.Form("mLoginPwd")
                    Dim token As String = Request.Form("token")

                    Response.Clear()
                    If String.IsNullOrEmpty(txtUid) OrElse txtUid.Length < 3 OrElse String.IsNullOrEmpty(txtPwd) OrElse txtPwd.Length < 3 Then
                        Response.Write(Util.ReturnValue("101", "请先填写用户名及密码(长度不够)!!!"))
                    Else
                        Response.Write(Login(txtUid, txtPwd, token))
                    End If
                    Response.End()

                Case "postlogin"
                    'Call IsBasePath() '检查路径
                    If Not ChkVaidCode() Then
                        Response.Clear()
                        Response.Write(Text.Alert("103,请重新填写验证码(验证码错误)!!", True))
                        Response.End()
                    End If
                    Dim txtUid As String = Request.Form("mLoginUid")
                    Dim txtPwd As String = Request.Form("mLoginPwd")
                    Dim token As String = Request.Form("token")
                    Dim toUrl As String = Request.Form("toUrl")

                    Response.Clear()
                    If String.IsNullOrEmpty(txtUid) OrElse txtUid.Length < 3 OrElse String.IsNullOrEmpty(txtPwd) OrElse txtPwd.Length < 3 Then
                        Response.Write(Text.Alert("请先填写用户名及密码(长度不够)!!!", True))
                        Response.End()
                    Else
                        Dim RtnValue As String() = Split(Login(txtUid, txtPwd, token), ",")
                        If RtnValue(0) = "0" Then
                            If String.IsNullOrEmpty(toUrl) Then
                                If Not Request.UrlReferrer Is Nothing Then
                                    toUrl = Request.UrlReferrer.AbsoluteUri
                                Else
                                    toUrl = "/"
                                End If

                            End If
                            Response.Redirect(toUrl)
                            Response.End()
                        Else
                            Response.Write(Text.Alert(RtnValue(1), True))
                            Response.End()
                        End If
                    End If

                Case "logout" REM 检测系统登录

                    LogOut(Request.QueryString("toUrl"))

                Case "cityflypost"
                    REM 返回的格式为: 代码,提示信息
                    REM 错误码 1,2|成功 ,0|非本站提交 ,100|数据库出错 ,404|文件找不到,500|未知错误
                    REM 101|字段不符合要求, 102|特定参数不存在 ,103|验证码失效 ,104|XML文件解析出错
                    REM 检查登录状态
                    Call CheckAdminLoginStatus()
                    Dim fTableLabelName As String = Request.Form("f")

                    If String.IsNullOrEmpty(fTableLabelName) Then
                        Response.Clear()
                        Response.Write(Util.ReturnValue("102", "必需的参数不存在!!"))
                        Response.End()
                    End If

                    SavePostContent(fTableLabelName)

                Case "relogin" REM 检测系统登录
                    LogOut(AppConfigInfo.AdminLoginPage)
                Case "toexcel" REM To EXCEL
                    Call IsBasePath()
                    ToExcel(getHtmlData(Request.QueryString("f")))
                Case Else

                    REM 检查路径
                    Call IsBasePath()



                    If IsXmlHttp Then
                        REM 检查登录状态
                        'Call CheckAdminLoginStatus()

                        Response.Write(getHtmlData(Request.QueryString("f")))
                        Response.End()
                    Else
                        '检查页面菜单权限

                        If Admin.IsLogin AndAlso Not ChkAdminRights(LabelInfo.MID, "ACL_Run") Then
                            Response.Status = String.Format("403 Access Deny (MID={0})", LabelInfo.MID)
                            Response.End()
                        End If

                        If Request.QueryString("toexcel") = "1" Then
                            ToExcel(getHtmlData(Request.QueryString("f")))
                        Else
                            WriteOutHtml()
                        End If
                    End If
            End Select
        End Sub

    End Class

End Namespace

